<?php
$db = new Database;
$auth = Auth::getAuth('current_user');
$user_id = $auth['id'];
$company_id = $auth['company_id'];
$user_groupType = $auth['role_type'];
$user_group = $auth['role'];
$userLevel = $auth['userlevel_id'];
$userPositionID = $auth['position_id'];
$userName = $auth['firstname'] . " " . $auth['lastname'];
$getPosition = $db->query("SELECT * FROM tbposition where id = '$userPositionID'","row");
$timezone = "Asia/Manila";
if(function_exists('date_default_timezone_set')) date_default_timezone_set($timezone);
if(isset($_POST['action'])){
    if($_POST['action']=="getAnnouncementPost"){
        $announcement =  stripslashes(htmlspecialchars($_POST['announcement'],ENT_QUOTES));
        $insert = array("company_id"=>$company_id,
                        "user_id"=>$user_id,
                        "announcement"=>$announcement,
                        "is_active"=>1,
                        "date"=>date("Y-m-d  H:i:s"));
        $postID = $db->insert("tbannouncement",$insert);
        // function to append new announcement
        echo announcementComment($postID,$user_id,$auth['extension'],$userName,$announcement,date("Y-m-d  H:i:s")); 
    }elseif($_POST['action']=="deletePost"){
        $dataID = $_POST['dataID'];
        // Delete Announcement
            $set = array("id"=>$dataID);
            $db->delete("tbannouncement",$set);
        // Delete Comment
            $delete = array("annoucement_id"=>$dataID);
            $db->delete("tbcomment",$delete);
        echo "Post Deleted.";
    }elseif($_POST['action']=="getPost"){
        $getPost = $db->query("SELECT * FROM tbannouncement WHERE company_id='$company_id' ORDER BY id DESC LIMIT 5","array");
        $count = $db->query("SELECT * FROM tbannouncement WHERE company_id='$company_id' ORDER BY id DESC","numrows");
        foreach($getPost as $data){
            $userID = $data['user_id'];
            $announcementID = $data['id'];
            $user = $db->query("SELECT * FROM tbuser WHERE id='$userID'","row");
            $userPostName = $user['firstname'] . " " . $user['lastname'];
            echo announcementComment($data['id'],$user['id'],$user['extension'],$userPostName,$data['announcement'],$data['date']); 
        }
        if($count>5){
        echo '<div class="more" id="more_'.$announcementID.'" data-id="'.$announcementID.'"style="float: left;border: 1px solid #ddd;margin-top: 10px; width: 100%;height: 30px;cursor: pointer;border-radius:5px;">
                    <div class="moreContent" style="text-align: center;margin-top: 8px;">More</div>
                </div>
                <div id="updates"></div>';
        }
    }elseif($_POST['action']=="addComment"){
        $commentID = $_POST['dataID'];
        $type = $_POST['type'];
        $newComment =  stripslashes(htmlspecialchars($_POST['newComment'],ENT_QUOTES));
        if($type!=1){
            $type = 2;
        }else{
            $type = 1;
        }
        $insert = array("user_id"=>$user_id,
                        "comment"=>$newComment,
                        "date"=>date("Y-m-d  H:i:s"),
                        "annoucement_id"=>$commentID,
                        "type"=>$type,
                        "is_active"=>1);
        $a = $db->insert("tbcomment",$insert);
        echo '<div id="deleteComment_'.$a.'">';
        echo myAvatar($auth['extension'],$auth['id'],"30","30","none");
        echo newComment($newComment,$a,$user_id,date("Y-m-d  H:i:s"));
        echo '<div>';
    }elseif($_POST['action']=="deleteAddedComment"){
        $dataID = $_POST['dataID'];
        // Delete Comment
            $delete = array("id"=>$dataID);
            $db->delete("tbcomment",$delete);
        echo "Post Deleted.";
    // Request to approved
    }elseif($_POST['action']=="getRequestToApprove"){
        $getRequest = $db->query("SELECT sf.numCol as numCol, sf.colValue_formID as colValue_formID,
                    r.date as date_request,r.user_id as user_id, f.id as form_id,
                 rp.wf_master_id as wf_master_id, f.form_title as form_title, r.data as data,
                 f.workflow_id as workflow_id,  f.description as description,
                 r.id as request_id,rp.id as requestProcess_id FROM tbrequest r
                 LEFT JOIN tbrequest_process rp on r.id=rp.request_id
                 LEFT JOIN tbform f on f.id=r.form_id
                 LEFT JOIN tbset_field sf on sf.companyID=f.company_id
                 WHERE (".roleApprover($user_groupType,$user_group)."
                 or rp.approver='".$auth['firstname']." ".$auth['lastname'] ."' or rp.approver='".$getPosition['position']."')
                 AND rp.status=0 and r.status=0 and r.company_id = $company_id ORDER BY r.id DESC","array");
        
        foreach($getRequest as $dataRequest){
            $buttons = "";
            $userID = $dataRequest['user_id'];
            $getUserRequest = $db->query("SELECT * FROM tbuser WHERE id='$userID'","row");
            //echo announcementComment($data['id'],$user['id'],$user['extension'],$userPostName,$data['announcement']);
            echo '
            <div class="requestBlock_'.$dataRequest['request_id'].'" style="float: left;width: 400px;border-bottom:1px solid #ddd;min-height: 30px; padding: 5px;list-style: none;">
                '.myAvatar($getUserRequest['extension'],$userID,"50","50","none").'
                <div style="float: left;width: 85%;padding-left: 5px;">
                    <div class="requestPost">
                    '.getUserWholeName($dataRequest['user_id']).'<br /> wants to approve their <u class="fontBlack">'.$dataRequest['form_title'].'</u> .
                    </div>
                    <div class="requestContentPost">
                    '.$dataRequest['description'].'
                    </div>
                    <div class="requestButtonPost">
                    ';
            $getButtonProperty = $db->query("SELECT * FROM tbworflow_obects wo LEFT JOIN tbworkflow w on wo.worflow_id = w.id where w.id='".$dataRequest['workflow_id']."' and wo.object_id='".$dataRequest['wf_master_id']."'","row");
            //buttons
            
            $arraySplit = substr($getButtonProperty['buttonStatus'],0,strlen($getButtonProperty['buttonStatus'])-1);
            $arraySplit = explode("+",$arraySplit);
            $buttons.="<span class='viewUserForm pointer' id='viewUserForm_".$dataRequest['request_id']."_".$dataRequest['requestProcess_id']."' object-id='".$nextApprover."' request_status='".$requestStatus."' sequence-approver='".$dataRequest['wf_master_id']."' button-properies='".$getButtonProperty['buttonStatus']."'>View</span> | ";
            for($a=0;$a<count($arraySplit);$a++){
                 $buttonID =  explode(":",$arraySplit[$a]);
                 $nextApprover = explode(">",$buttonID[1]);
                 $requestStatus = $nextApprover[1];
                 $buttonID = $buttonID[0];
                 $nextApprover = $nextApprover[0];
                 $getButton = $db->query("SELECT * FROM tbbuttons where id = $buttonID","row");
                 $buttons.="<span class='approverButton pointer' id='button_".$dataRequest['request_id']."_".$dataRequest['requestProcess_id']."'
                                object-id='".$nextApprover."' request_status='".$requestStatus."' sequence-approver='".$dataRequest['wf_master_id']."'>
                                ".$getButton['button']."</span> | ";
            }
            echo substr($buttons,0,strlen($buttons)-2);
            echo '<label class="fontGray timeago pull-right" style="margin-right:15px;font-size:10px;" title="'.$dataRequest['date_request'].'"></label>';
            echo '</div>';
            echo '
            <div style="float: left;background-color: whiteSmoke;padding: 5px;width: 345px;" id="addComment_'.$dataRequest['request_id'].'_2">
                    ';
                    // Function to get your additional comment
                    echo addComment($dataRequest['request_id'],$dataRequest['request_id'],2,"LIMIT 4"," ");
                    echo '<div class="appendNewComment" id="appendNewComment_'.$dataRequest['request_id'].'_2"></div>';
            echo '
                    <div style="float: left;width: 10%;">';
                    //  Function to get your avatar
                        echo myAvatar($auth['extension'],$user_id,"30","30","none");
                        
            echo '                   
                    </div>
                    <div style="float: left;width: 85%;padding-left: 5px;">
                        <textarea class="input-comment" id="newComment_'.$dataRequest['request_id'].'_2" style="height:30px;width:300px;"></textarea><br />
                        <input type="button" class="button-icon addButtonComment" data-comment-type="2" value="Comment" data-id="'.$dataRequest['request_id'].'">
                    </div>
            </div>';
            echo '
                </div>
            </div>';
        }
        
    }
    // Load More Request
    elseif($_POST['action']=="loadMore"){
        $loadID = $_POST['id'];
        $getPost = $db->query("SELECT * FROM tbannouncement WHERE id<'$loadID' AND company_id='$company_id' ORDER BY id DESC LIMIT 10","array");
        foreach($getPost as $data){
            $userID = $data['user_id'];
            $announcementID = $data['id'];
            $user = $db->query("SELECT * FROM tbuser WHERE id='$userID'","row");
            $userPostName = $user['firstname'] . " " . $user['lastname'];
            echo announcementComment($data['id'],$user['id'],$user['extension'],$userPostName,$data['announcement'],$data['date']); 
        }
        echo '<div id="more_'.$announcementID.'" class="more" data-id="'.$announcementID.'"style="float: left;border: 1px solid #ddd;margin-top: 10px; width: 100%;height: 30px;cursor: pointer;border-radius:5px;">
                    <div class="moreContent" style="text-align: center;margin-top: 8px;">More</div>
                </div>
                <div id="updates"></div>';
    }
    // Show More Commnet
    elseif($_POST['action']=="loadCommentMore"){
        $id = $_POST['id'];
        $type = $_POST['type'];
        echo addComment($id,$id,$type,"","display");
    }
}
?>